/*
 * Lucas McDaniel
 * GSoC '11
 * HoneyViz
 * 
 * 
 */

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import org.w3c.dom.*;
import javax.swing.tree.*;
import javax.xml.parsers.*;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import org.jfree.chart.*;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.*;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author Lucas
 */
public class HoneyViz extends javax.swing.JApplet {

    /** Initializes the applet NewJApplet1 */
    public void init() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            java.awt.EventQueue.invokeAndWait(new Runnable() {

                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        category_Tree("");
        jTree1 = new javax.swing.JTree();
        jTextField1 = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jProgressBar1 = new javax.swing.JProgressBar();
        jSlider1 = new javax.swing.JSlider();
        jPanel2 = new javax.swing.JPanel();
        initConnection();
        jScrollPane2 = new javax.swing.JScrollPane(createBarGraph());
        jPanel3 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane(createLineGraph());
        jToggleButton1 = new javax.swing.JToggleButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem17 = new javax.swing.JMenuItem();
        jSeparator4 = new javax.swing.JPopupMenu.Separator();
        jMenuItem19 = new javax.swing.JMenuItem();
        jMenuItem18 = new javax.swing.JMenuItem();
        jSeparator5 = new javax.swing.JPopupMenu.Separator();
        jMenu8 = new javax.swing.JMenu();
        jMenu9 = new javax.swing.JMenu();
        jMenuItem24 = new javax.swing.JMenuItem();
        jMenuItem25 = new javax.swing.JMenuItem();
        jMenu10 = new javax.swing.JMenu();
        jMenuItem22 = new javax.swing.JMenuItem();
        jMenuItem23 = new javax.swing.JMenuItem();
        jMenu11 = new javax.swing.JMenu();
        jMenuItem15 = new javax.swing.JMenuItem();
        jMenuItem16 = new javax.swing.JMenuItem();
        jMenu12 = new javax.swing.JMenu();
        jMenuItem20 = new javax.swing.JMenuItem();
        jMenuItem21 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jMenu5 = new javax.swing.JMenu();
        jMenuItem9 = new javax.swing.JMenuItem();
        jMenuItem10 = new javax.swing.JMenuItem();
        jMenuItem11 = new javax.swing.JMenuItem();
        jMenuItem12 = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        jMenu6 = new javax.swing.JMenu();
        jMenuItem13 = new javax.swing.JMenuItem();
        jMenuItem14 = new javax.swing.JMenuItem();
        jMenuItem8 = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JPopupMenu.Separator();
        jMenu7 = new javax.swing.JMenu();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();

        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        
        jTree1.setModel(new DefaultTreeModel(tree));
        jTree1.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                jTree1ValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jTree1);

        jTextField1.setText("Search...");
        jTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextField1FocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextField1FocusLost(evt);
            }
        });
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField1KeyReleased(evt);
            }
        });

        jScrollPane4.setViewportView(new MapJPanel());
        jScrollPane4.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        jScrollPane4.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jScrollPane4MousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jScrollPane4MouseReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 714, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE)
                .addContainerGap())
        );

        jSlider1.setValue(0);
        jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSlider1StateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
                .addContainerGap())
        );

        jScrollPane3.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 688, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
                .addContainerGap())
        );

        jToggleButton1.setText("Individual");
        jToggleButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jToggleButton1MouseClicked(evt);
            }
        });

        jButton1.setIcon(play);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(stop);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jMenu1.setText("File");

        jMenuItem1.setText("Database Access");
        jMenu1.add(jMenuItem1);
        jMenu1.add(jSeparator1);

        jMenu3.setText("Save As...");
        jMenu3.setActionCommand("Save");

        jMenuItem2.setText("World Map");
        jMenu3.add(jMenuItem2);

        jMenuItem3.setText("Bar Graph");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem3);

        jMenuItem4.setText("Line Graph");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem4);

        jMenuItem5.setText("Custom URL");
        jMenu3.add(jMenuItem5);

        jMenu1.add(jMenu3);

        //jMenuBar1.add(jMenu1);

        jMenu2.setText("Views");

        jMenuItem17.setText("Color Chooser");
        jMenuItem17.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem17ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem17);
        jMenu2.add(jSeparator4);

        jMenuItem19.setText("Standard View");
        jMenuItem19.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem19ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem19);

        jMenuItem18.setText("Honeycomb View");
        jMenuItem18.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem18ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem18);
        jMenu2.add(jSeparator5);

        jMenu8.setText("Custom Visualization");

        jMenu9.setText("Primary Visualization");

        jMenuItem24.setText("Map View");
        jMenuItem24.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem24ActionPerformed(evt);
            }
        });
        jMenu9.add(jMenuItem24);

        jMenuItem25.setText("Honeycomb View");
        jMenuItem25.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem25ActionPerformed(evt);
            }
        });
        jMenu9.add(jMenuItem25);

        jMenu8.add(jMenu9);

        jMenu10.setText("Secondary Visualization");

        jMenuItem22.setText("Line Graph");
        jMenuItem22.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem22ActionPerformed(evt);
            }
        });
        jMenu10.add(jMenuItem22);

        jMenuItem23.setText("Bar Graph");
        jMenuItem23.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem23ActionPerformed(evt);
            }
        });
        jMenu10.add(jMenuItem23);

        jMenu8.add(jMenu10);

        jMenu11.setText("Statistics Page");

        jMenuItem16.setText("Event Statistics");
        jMenuItem16.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem16ActionPerformed(evt);
            }
        });
        jMenu11.add(jMenuItem16);

        jMenuItem15.setText("Location Statistics");
        jMenuItem15.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem15ActionPerformed(evt);
            }
        });
        jMenu11.add(jMenuItem15);

        jMenu8.add(jMenu11);

        jMenu12.setText("Search Field");

        jMenuItem20.setText("Event Search");
        jMenuItem20.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem20ActionPerformed(evt);
            }
        });
        jMenu12.add(jMenuItem20);

        jMenuItem21.setText("Location Search");
        jMenuItem21.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem21ActionPerformed(evt);
            }
        });
        jMenu12.add(jMenuItem21);

        jMenu8.add(jMenu12);

        jMenu2.add(jMenu8);

        jMenuBar1.add(jMenu2);

        jMenu4.setText("Specifications");

        jMenu5.setText("Time Interval");

        jMenuItem9.setText("Day");
        jMenuItem9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem9ActionPerformed(evt);
            }
        });
        jMenu5.add(jMenuItem9);

        jMenuItem10.setText("Week");
        jMenuItem10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem10ActionPerformed(evt);
            }
        });
        jMenu5.add(jMenuItem10);

        jMenuItem11.setText("Month");
        jMenuItem11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem11ActionPerformed(evt);
            }
        });
        jMenu5.add(jMenuItem11);

        jMenuItem12.setText("All");
        jMenuItem12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem12ActionPerformed(evt);
            }
        });
        jMenu5.add(jMenuItem12);

        jMenu4.add(jMenu5);
        jMenu4.add(jSeparator2);

        jMenu6.setText("Statistics");

        jMenuItem13.setText("Total Count");
        jMenuItem13.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem13ActionPerformed(evt);
            }
        });
        jMenu6.add(jMenuItem13);

        jMenuItem14.setText("Distinct Count");
        jMenuItem14.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem14ActionPerformed(evt);
            }
        });
        jMenu6.add(jMenuItem14);

        jMenuItem8.setText("Cumulative");
        jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem8ActionPerformed(evt);
            }
        });
        jMenu6.add(jMenuItem8);

        jMenu4.add(jMenu6);
        jMenu4.add(jSeparator3);

        jMenu7.setText("Map Underlay");

        jMenuItem6.setText("Geographical");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu7.add(jMenuItem6);

        jMenuItem7.setText("Political");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        jMenu7.add(jMenuItem7);

        jMenu4.add(jMenu7);

        jMenuBar1.add(jMenu4);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(22, 22, 22)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(32, 32, 32)
                                .addComponent(jToggleButton1))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jProgressBar1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 734, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jSlider1, javax.swing.GroupLayout.DEFAULT_SIZE, 646, Short.MAX_VALUE))
                            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 371, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton2)
                        .addComponent(jButton1))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jToggleButton1)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        loadDefault();
    }// </editor-fold>//GEN-END:initComponents

    public void loadDefault()
    {
        TreePath root = new TreePath(jTree1.getModel().getRoot());

        ArrayList rtnVal = getPaths(root);
        
        TreePath[] select = new TreePath[rtnVal.size()];
        rtnVal.toArray(select);
        jTree1.setSelectionPaths(select);
        
        AppletState.setGroupToCategory(true);
        AppletState.setHeat(true);
        
        redraw();
    }
    
    public ArrayList getPaths(TreePath node)
    {
        ArrayList rtnVal = new ArrayList();
        if (((DefaultMutableTreeNode)node.getLastPathComponent()).isLeaf())
        {
            rtnVal.add(node);
            String tmp = (String)((DefaultMutableTreeNode)node.getLastPathComponent()).getUserObject();
            if (tmp.length()>10)
                return rtnVal;
            else
                return new ArrayList();
        }
        else
        {
            Enumeration e = ((DefaultMutableTreeNode)node.getLastPathComponent()).children();
            while (e.hasMoreElements())
                rtnVal.addAll(getPaths(new TreePath(((DefaultMutableTreeNode)e.nextElement()).getPath())));
            return rtnVal;
        }
    }
    
    public void initConnection()
    {
        try
        {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new URL(AppletState.url + "index.php").openStream());
            HoneyData.add(doc.getElementsByTagName("result"));
            
            setSize(new Dimension(1120, 780));
            
            timer = new Timer(1000/AppletState.FPS, new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    timerActionListener(evt);
                }
            });
            
            timer.stop();
            
            jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
                public void stateChanged(ChangeEvent evt) {
                    jSlider1StateChanged(evt);
                }
            });
            AppletState.firstLoad();
            
            stop = new ImageIcon(new URL(AppletState.url + "images/stop.gif"));
            play = new ImageIcon(new URL(AppletState.url + "images/play.gif"));
            pause = new ImageIcon(new URL(AppletState.url + "images/pause.gif"));
        }
        catch (Exception e) {e.printStackTrace();}
    }
    
    private JTable createStatsPanel()
    {
        ArrayList nameList = EventColorManager.getNameList();
        
        if (nameList.size()==0)
            return null;
        String[] events = new String[nameList.size()];
        nameList.toArray(events);
        ArrayList result = new ArrayList();
        boolean tempStore = AppletState.isHeat();
        AppletState.setHeat(false);
        AppletState.setHeat(tempStore);
        if (AppletState.isInStatsCache(nameList))
            result = AppletState.getStatsCache();
        else if (AppletState.isDistinct() && AppletState.isGroupToCategory())
        {
            if (AppletState.getPrimarySearch()==0)
                result = HoneyData.groupBy(events, null, null, false, true, false);
            else
                result = HoneyData.groupBy(null, null, LocationManager.getLocations(events), false, true, false);
            AppletState.makeStatsCache(nameList, result);
        }
        else if (AppletState.isDistinct())
        {
            if (AppletState.getPrimarySearch()==0)
            {
                if (AppletState.getPrimaryStat()==0)
                    result = HoneyData.groupBy(events, null, null, true, false, false);
                else
                    result = HoneyData.groupBy(events, null, null, false, false, true);
            }
            else
            {
                if (AppletState.getPrimaryStat()==1)
                    result = HoneyData.groupBy(null, null, LocationManager.getLocations(events), false, false, true);
                else
                    result = HoneyData.groupBy(null, null, LocationManager.getLocations(events), true, false, false);
            }
            AppletState.makeStatsCache(nameList, result);
        }
        else if (AppletState.isGroupToCategory())
        {
            if (AppletState.getPrimarySearch()==0)
                result = HoneyData.groupBy(events, null, null, false, true, false);
            else
                result = HoneyData.groupBy(null, null, LocationManager.getLocations(events), false, true, false);
            for (int inc=0; inc<result.size(); inc++)
                ((DataElement)result.get(inc)).name = "Group";
            AppletState.makeStatsCache(nameList, result);
        }
        else
        {
            if (AppletState.getPrimarySearch()==0)
            {
                if (AppletState.getPrimaryStat()==0)
                    result = HoneyData.groupBy(events, null, null, true, false, false);
                else
                    result = HoneyData.groupBy(events, null, null, false, false, true);
            }
            else
            {
                if (AppletState.getPrimaryStat()==1)
                    result = HoneyData.groupBy(null, null, LocationManager.getLocations(events), false, false, true);
                else
                {
                    int tmp = AppletState.getPrimarySearch();
                    AppletState.setPrimarySearch(0);
                    result = HoneyData.groupBy(null, null, LocationManager.getLocations(events), true, false, false);
                    AppletState.setPrimarySearch(tmp);
                }
            }
            AppletState.makeStatsCache(nameList, result);
        }
        
        String[] colNames; // = { "Location", "Event 1", "Event 2", "Event 3"};
        Object[][] data;
        
        if (AppletState.isGroupToCategory())
        {
            colNames = new String[2];
            colNames[0] = "Date";
            colNames[1] = "Quantity";
            int counter = 0;
            boolean b = AppletState.isHeat();
            data = new Object[result.size()+(b?0:1)][2];
            SimpleDateFormat format = new SimpleDateFormat("MMM dd, yyyy");
            for (int inc=0; inc<result.size(); inc++)
            {
                DataElement DE = (DataElement)result.get(inc); 
                data[inc][0] = format.format(DE.time);
                data[inc][1] = DE.count;
                counter += DE.count;
            }
            if (!b)
            {
                data[result.size()][0] = "Total";
                data[result.size()][1] = counter;    
            }
            AppletState.setHeat(tempStore);
            JTable table = new JTable(data, colNames);
            table.setEnabled(false);
            return table;
        }
        
        ArrayList largestEvents = new ArrayList();
        
        for (int inc=0; inc<result.size(); inc++)
        {
            DataElement DE = (DataElement)result.get(inc);
            boolean found = false;
            for (int inc2=0; inc2<largestEvents.size(); inc2++)
            {
                if (DE.count > ((DataElement)largestEvents.get(inc2)).count)
                {
                    largestEvents.add(inc2, DE);
                    found = true;
                    break;
                }
            }
            if (!found)
                largestEvents.add(DE);
            if (largestEvents.size() > 3)
                largestEvents.remove(largestEvents.size()-1);
        }
        
        events = new String[largestEvents.size()];
        for (int inc=0; inc<largestEvents.size(); inc++)
        {
            if (AppletState.getPrimaryStat()==0)
                events[inc] = ((DataElement)largestEvents.get(inc)).name;
            else
                events[inc] = LocationManager.getLocationName((DataElement)largestEvents.get(inc));
        }
        if (AppletState.getPrimaryStat()==0)
        {
            result = HoneyData.groupBy(events, null, null, false, false, true);
        }
        else
        {
            int tmp = AppletState.getPrimarySearch();
            AppletState.setPrimarySearch(0);
            result = HoneyData.groupBy(null, null, LocationManager.getLocations(events), true, false, false);
            AppletState.setPrimarySearch(tmp);
        }
        if (result.size()==0)
        {
            AppletState.setHeat(tempStore);
            return null;
        }
        int maxSize = events.length<3?events.length:3;
        
        colNames = new String[maxSize+1];
        
        if (AppletState.getPrimaryStat()==0)
            colNames[0] = "Event";
        else
            colNames[0] = "Location";
        
        for (int inc=0; inc<maxSize; inc++)
        {
            if (AppletState.getPrimaryStat()==0)
                colNames[inc+1] = events[inc].substring(0, 6);
            else
                colNames[inc+1] = events[inc];
        }
        
            
        data = new Object[result.size()][maxSize+1];
        
        for (int inc=0; inc<result.size(); inc++)
        {
            int store = inc;
            for (int inc2=inc+1; inc2<result.size(); inc2++)
                if (((DataElement)result.get(store)).count < ((DataElement)result.get(inc2)).count)
                    store = inc2;
            Collections.swap(result, inc, store);
            if (AppletState.getPrimaryStat()==0)
                data[inc][0] = LocationManager.getLocationName((DataElement)result.get(inc));
            else
                data[inc][0] = ((DataElement)result.get(inc)).name;
        }
        
        if (AppletState.getPrimaryStat()==0)
        {
            int tmp = AppletState.getPrimarySearch();
            AppletState.setPrimarySearch(0);
            result = HoneyData.groupBy(events, null, null, true, false, true);
            AppletState.setPrimarySearch(tmp);
        }
        else
        {
            result = HoneyData.groupBy(null, null, LocationManager.getLocations(events), true, false, true);
            double[][] tmpArr = LocationManager.getLocations(events);
        }
        
        for (int inc=0; inc<result.size(); inc++)
        {
            DataElement DE = (DataElement)result.get(inc);
            for (int inc2=0; inc2<data.length; inc2++)
            {
                if (AppletState.getPrimaryStat()==0)
                {
                    if (((String)data[inc2][0]).compareTo(LocationManager.getLocationName(DE))==0)
                    {
                        for (int i=0; i<maxSize; i++)
                            if (events[i].compareTo(DE.name)==0)
                                data[inc2][1+i] = DE.count;
                        break;
                    }
                }
                else
                {
                    if (((String)data[inc2][0]).compareTo(DE.name)==0)
                    {
                        for (int i=0; i<maxSize; i++)
                            if (events[i].compareTo(LocationManager.getLocationName(DE))==0)
                                data[inc2][1+i] = DE.count;
                        break;
                    }
                }
            }
        }
        
        
        
        AppletState.setHeat(tempStore);
        JTable table = new JTable(data, colNames);
        table.setEnabled(false);
        return table;
    }
    
    private ChartPanel createBarGraph()
    {
        ArrayList nameList = EventColorManager.getNameList();
        
        if (nameList.size()==0)
            return null;
        
        String[] events = new String[nameList.size()];
        nameList.toArray(events);
        ArrayList result = new ArrayList();
        boolean tempStore = AppletState.isHeat();
        AppletState.setHeat(false);
        if (AppletState.isInBarCache(nameList))
            result = AppletState.getBarCache();
        else if (AppletState.isDistinct())
        {
            if (AppletState.getPrimarySearch()==0)
                result = HoneyData.groupBy(events, null, null, true, false, false);
            else
                result = HoneyData.groupBy(null, null, LocationManager.getLocations(events), false, false, true);
            AppletState.makeBarCache(nameList, result);
        }
        else if (AppletState.isGroupToCategory())
        {
            if (AppletState.getPrimarySearch()==0)
                result = HoneyData.groupBy(events, null, null, false, false, false);
            else
                result = HoneyData.groupBy(null, null, LocationManager.getLocations(events), false, false, false);
            for (int inc=0; inc<result.size(); inc++)
                ((DataElement)result.get(inc)).name = "Group";
            AppletState.makeBarCache(nameList, result);
        }
        else
        {
            if (AppletState.getPrimarySearch()==0)
                result = HoneyData.search(events, null, null);
            else
                result = HoneyData.search(null, null, LocationManager.getLocations(events));
            AppletState.makeBarCache(nameList, result);
        }
        AppletState.setHeat(tempStore);
        if (result.size()==0)
            return null;
        ArrayList name_list = new ArrayList();
        ArrayList count_list = new ArrayList();
        String current = "";
        if (AppletState.getPrimarySearch()==0)
            current = ((DataElement)result.get(0)).name;
        else
            current = LocationManager.getLocationName((DataElement)result.get(0));
        int count = 0;
        
        for (int inc=0; inc<result.size(); inc++)
        {
            if (AppletState.getPrimarySearch()==0)
            {
                if (((DataElement)result.get(inc)).name.compareTo(current)==0)
                    count += ((DataElement)result.get(inc)).count;
                else
                {
                    name_list.add(current);
                    count_list.add(count);
                    current = ((DataElement)result.get(inc)).name;
                    count = ((DataElement)result.get(inc)).count;
                }
            }
            else
            {
                if (LocationManager.getLocationName((DataElement)result.get(inc)).compareTo(current)==0)
                    count += ((DataElement)result.get(inc)).count;
                else
                {
                    name_list.add(current);
                    count_list.add(count);
                    current = LocationManager.getLocationName((DataElement)result.get(inc));
                    count = ((DataElement)result.get(inc)).count;
                } 
            }
        }
        
        name_list.add(current);
        count_list.add(count);
        
        ArrayList name_list2 = new ArrayList();
        ArrayList count_list2 = new ArrayList();
        
        int max_bar = 10;
        
        for (int inc=0; inc<count_list.size(); inc++)
        {
            if (count_list2.size()<10)
            {
                count_list2.add(count_list.get(inc));
                name_list2.add(name_list.get(inc));
            }
            else
            {
                int indexOfSmallest = 0;
                for (int inc2=1; inc2<count_list2.size(); inc2++)
                {
                    if ((Integer)count_list2.get(inc2)<(Integer)count_list2.get(indexOfSmallest))
                        indexOfSmallest = inc2;
                }
                if ((Integer)count_list.get(inc)>(Integer)count_list2.get(indexOfSmallest))
                {
                    count_list2.remove(indexOfSmallest);
                    name_list2.remove(indexOfSmallest);
                    count_list2.add(count_list.get(inc));
                    name_list2.add(name_list.get(inc));
                }
            }
        }
        
        name_list = name_list2;
        count_list = count_list2;
        
        final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        for (int inc=0; inc<name_list.size(); inc++)
            dataset.addValue(((Integer)count_list.get(inc)).intValue(), (String)name_list.get(inc), (String)name_list.get(inc));
        
        JFreeChart chart = ChartFactory.createBarChart("Selected Events", "Events", "Count", dataset, PlotOrientation.VERTICAL, false, false, false);
        CategoryPlot plot = chart.getCategoryPlot();
        BarRenderer renderer = (BarRenderer)plot.getRenderer();
        renderer.setItemMargin(0.01);
        
        Color[] arrColor = EventColorManager.getPaints();
        
        for (int inc=0; inc<name_list.size(); inc++)
        {
            renderer.setSeriesPaint(inc, EventColorManager.getEvent((String)name_list.get(inc)).paint);
        }
        ChartPanel rtnVal = new ChartPanel(chart);
        rtnVal.setPreferredSize(new Dimension(300,300));
        currentBarChart = rtnVal;
        return rtnVal;
    }
    
    private ChartPanel createLineGraph()
    {
        try
        {
            ArrayList nameList = EventColorManager.getNameList();
        
            if (nameList.size()==0)
                return null;

            String[] events = new String[nameList.size()];
            nameList.toArray(events);
            
            ArrayList result = new ArrayList();

            if (AppletState.isInLineCache(nameList))
            {
                result = AppletState.getLineCache();
            }
            else if (AppletState.isDistinct())
            {
                if (AppletState.getPrimarySearch()==0)
                    result = HoneyData.groupBy(events, null, null, true, true, false);
                else
                    result = HoneyData.groupBy(null, null, LocationManager.getLocations(events), false, true, true);
                AppletState.makeLineCache(nameList, result);
                
            }
            else if (AppletState.isGroupToCategory())
            {
                if (AppletState.getPrimarySearch()==0)
                    result = HoneyData.groupBy(events, null, null, false, true, false);
                else
                {
                    result = HoneyData.groupBy(null, null, LocationManager.getLocations(events), false, true, false);
                }
                for (int inc=0; inc<result.size(); inc++)
                    ((DataElement)result.get(inc)).name = "Group";
                AppletState.makeLineCache(nameList, result);
            }
            else
            {
                if (AppletState.getPrimarySearch()==0)
                    result = HoneyData.groupBy(events, null, null, true, true, false);
                else
                {
                    result = HoneyData.groupBy(null, null, LocationManager.getLocations(events), false, true, true);
                }
                AppletState.makeLineCache(nameList, result);
            }
            
            
            ArrayList time_name = new ArrayList();
            ArrayList time_count = new ArrayList();
            ArrayList time_time = new ArrayList();
            
            if (result.size()==0)
                return null;
            
            String name = "";
            if (AppletState.getPrimarySearch()==0)
                name = ((DataElement)result.get(0)).name;
            else
                name = LocationManager.getLocationName((DataElement)result.get(0));
            int count = ((DataElement)result.get(0)).count;
            Date time = ((DataElement)result.get(0)).time;
            
            Calendar c1 = Calendar.getInstance();
            Calendar c2 = Calendar.getInstance();
            c1.setTime(time);
            c2.setTime(time);
            Date d1 = c1.getTime();
            Date d2 = c2.getTime();
            
            for (int inc=1; inc<result.size(); inc++)
            {
                DataElement DE = (DataElement)result.get(inc);
                c1.setTime(DE.time);
                if (AppletState.getTimeInterval() == 1)
                {
                    c1.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
                    c2.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
                }
                else if (AppletState.getTimeInterval() == 2)
                {
                    c1.set(Calendar.DAY_OF_MONTH, 1);
                    c2.set(Calendar.DAY_OF_MONTH, 1);
                }
                else if (AppletState.getTimeInterval() == 3)
                {
                    c1.set(Calendar.DAY_OF_YEAR, 1);
                    c2.set(Calendar.DAY_OF_YEAR, 1);
                }
                d1 = c1.getTime();
                d2 = c2.getTime();
                if (AppletState.getPrimarySearch()==0)
                {
                    if (DE.name.compareTo(name)==0 && d1.compareTo(d2)==0)
                        count += DE.count;
                    else
                    {
                        time_name.add(name);
                        time_count.add(count);
                        time_time.add(d2);
                        name = DE.name;
                        count = DE.count;
                        time = DE.time;
                        c2.setTime(time);
                        if (AppletState.getTimeInterval() == 1)
                            c2.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
                        else if (AppletState.getTimeInterval() == 2)
                            c2.set(Calendar.DAY_OF_MONTH, 1);
                        else if (AppletState.getTimeInterval() == 3)
                            c2.set(Calendar.DAY_OF_YEAR, 1);
                        d2 = c2.getTime();
                    }
                }
                else
                {
                    if (LocationManager.getLocationName(DE).compareTo(name)==0 && d1.compareTo(d2)==0)
                        count += DE.count;
                    else
                    {
                        time_name.add(name);
                        time_count.add(count);
                        time_time.add(d2);
                        name = LocationManager.getLocationName(DE);
                        count = DE.count;
                        time = DE.time;
                        c2.setTime(time);
                        if (AppletState.getTimeInterval() == 1)
                            c2.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
                        else if (AppletState.getTimeInterval() == 2)
                            c2.set(Calendar.DAY_OF_MONTH, 1);
                        else if (AppletState.getTimeInterval() == 3)
                            c2.set(Calendar.DAY_OF_YEAR, 1);
                        d2 = c2.getTime();
                    }
                }
            }
            
            time_name.add(name);
            time_count.add(count);
            time_time.add(d2);
            
            if (time_time.size()==0)
                return null;
            Date first = (Date)time_time.get(0);
            Date last = first;

            for (int inc=0; inc<time_time.size(); inc++)
            {
                Date tmp = (Date)time_time.get(inc);
                if (first.after(tmp))
                    first = tmp;
                else if (last.before(tmp))
                    last = tmp;
            }
            if (first.compareTo(last)==0)
            {
                c1.setTime(first);
                c2.setTime(last);
                if (AppletState.getTimeInterval() == 0)
                {
                    c1.add(Calendar.DATE, -1);
                    c2.add(Calendar.DATE, 1);    
                }
                else if (AppletState.getTimeInterval() == 1)
                {
                    c1.add(Calendar.WEEK_OF_YEAR, -1);
                    c2.add(Calendar.WEEK_OF_YEAR, 1);
                }
                else if (AppletState.getTimeInterval() == 2)
                {
                    c1.add(Calendar.MONTH, -1);
                    c2.add(Calendar.MONTH, 1);
                }
                else if (AppletState.getTimeInterval() == 3)
                {
                    c1.add(Calendar.YEAR, -1);
                    c2.add(Calendar.YEAR, 1);
                }
                first = c1.getTime();
                last = c2.getTime();
            }
            ArrayList series = new ArrayList();
            for (int inc=0; inc<time_name.size(); inc++)
            {
                String event_name = (String)time_name.get(inc);
                boolean found = false;
                for (int inc2=0; inc2<series.size(); inc2++)
                {
                    if (((XYSeries)series.get(inc2)).getKey().compareTo(event_name)==0)
                    {
                        int tmpIndex = ((XYSeries)series.get(inc2)).indexOf(((Date)time_time.get(inc)).getTime());
                        if (tmpIndex >= 0)
                            ((XYSeries)series.get(inc2)).remove(tmpIndex);
                        ((XYSeries)series.get(inc2)).add(((Date)time_time.get(inc)).getTime(), ((Integer)time_count.get(inc)).intValue());
                        found = true;
                        break;
                    }
                }
                if (!found)
                {
                    final XYSeries s = new XYSeries(event_name);
                    Date dateInc = first;
                    while (!dateInc.after(last))
                    {
                        s.add(dateInc.getTime(), 0);
                        c1.setTime(dateInc);
                        if (AppletState.getTimeInterval() == 0)
                            c1.add(Calendar.DATE, 1);
                        else if (AppletState.getTimeInterval() == 1)
                            c1.add(Calendar.WEEK_OF_YEAR, 1);
                        else if (AppletState.getTimeInterval() == 2)
                            c1.add(Calendar.MONTH, 1);
                        else if (AppletState.getTimeInterval() == 3)
                            c1.add(Calendar.YEAR, 1);
                        dateInc = c1.getTime();
                    }
                    int tmpIndex = s.indexOf(((Date)time_time.get(inc)).getTime());
                    if (tmpIndex >= 0)
                        s.remove(tmpIndex);
                    s.add(((Date)time_time.get(inc)).getTime(), (Integer)time_count.get(inc));
                    series.add(s);
                }
            }
            final XYSeriesCollection dataset = new XYSeriesCollection();
            for (int inc=0; inc<series.size(); inc++)
                dataset.addSeries((XYSeries)series.get(inc));
            String chartName = "Historical Data";
            if (AppletState.isHeat())
                chartName = "Cumulative Data";
            JFreeChart chart = ChartFactory.createTimeSeriesChart(chartName, "Date", "Count", dataset, false, false, false);
            XYPlot plot = (XYPlot)chart.getPlot();
            ArrayList distinctName = new ArrayList();
            distinctName.add(time_name.get(0));
            for (int inc=0; inc<time_name.size(); inc++)
                if (((String)distinctName.get(distinctName.size()-1)).compareTo((String)time_name.get(inc))!=0)
                    distinctName.add((String)time_name.get(inc));
            for (int inc=0; inc<distinctName.size(); inc++)
                plot.getRenderer().setSeriesPaint(inc, EventColorManager.getEvent((String)distinctName.get(inc)).paint);
            ChartPanel rtnVal = new ChartPanel(chart);
            rtnVal.setPreferredSize(new Dimension(300,300));
            currentLineChart = rtnVal;
            return rtnVal;
        }
        catch (Exception e)
        {e.printStackTrace();}
        return null;
    }
    
    private void location_Tree(String filter)
    {
        try
        {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new URL(AppletState.url + "location.php").openStream());
            
            NodeList list = doc.getElementsByTagName("item");
            
            String[] city = new String[list.getLength()];
            double[] latArr = new double[list.getLength()];
            double[] longArr = new double[list.getLength()];
            
            for (int inc=0; inc<list.getLength(); inc++)
            {
                Element result = (Element)list.item(inc);
                city[inc] = result.getElementsByTagName("city").item(0).getChildNodes().item(0).getNodeValue();
                latArr[inc] = Double.parseDouble(result.getElementsByTagName("latitude").item(0).getChildNodes().item(0).getNodeValue());
                longArr[inc] = Double.parseDouble(result.getElementsByTagName("longitude").item(0).getChildNodes().item(0).getNodeValue());
            }
            DefaultMutableTreeNode temp_tree = new DefaultMutableTreeNode("Locations");
            LocationManager.reset();
            if (filter.length() > 0)
            {
                for (int inc=0; inc<city.length; inc++)
                {
                    if (city[inc].toLowerCase().indexOf(filter.toLowerCase())!=-1)
                        temp_tree.add(new DefaultMutableTreeNode(city[inc]));
                    LocationManager.add(new Location(city[inc], latArr[inc], longArr[inc]));
                }
            }
            else
            {
                for (int inc=0; inc<city.length; inc++)
                {
                    temp_tree.add(new DefaultMutableTreeNode(city[inc]));
                    LocationManager.add(new Location(city[inc], latArr[inc], longArr[inc]));
                }
            }
            tree = temp_tree;
        }
        catch (Exception e) {e.printStackTrace();}
    }
        
    private void category_Tree(String filter)
    {
        if (tree == null)
            location_Tree("");
        try
        {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new URL(AppletState.url + "item.php").openStream());
            
            NodeList list = doc.getElementsByTagName("item");
            int[] idnum = new int[list.getLength()];
            int[] parent = new int[list.getLength()];
            String[] name = new String[list.getLength()];
            for (int inc=0; inc<list.getLength(); inc++)
            {
                Element result = (Element)list.item(inc);
                idnum[inc] = Integer.parseInt(result.getElementsByTagName("id").item(0).getChildNodes().item(0).getNodeValue());
                parent[inc] = Integer.parseInt(result.getElementsByTagName("parent").item(0).getChildNodes().item(0).getNodeValue());
                name[inc] = result.getElementsByTagName("name").item(0).getChildNodes().item(0).getNodeValue();
            }
            DefaultMutableTreeNode temp_tree = new DefaultMutableTreeNode(name[0]);
            createCategoryTree(1, temp_tree, idnum, parent, name);
            if (filter.length() > 0)
                applyCategoryFilter(temp_tree, filter);
            tree = temp_tree;
        }
        catch (Exception e) {e.printStackTrace();}
    }
    
    private void createCategoryTree(int parent, DefaultMutableTreeNode tree, int[] id, int[] par, String[] name)
    {
        for (int inc=0; inc<id.length; inc++)
        {
            if (par[inc]==parent)
            {
                DefaultMutableTreeNode dmtn = new DefaultMutableTreeNode(name[inc]);
                tree.add(dmtn);
                createCategoryTree(id[inc], dmtn, id, par, name);
            }
        }
    }
    
    private void applyCategoryFilter(DefaultMutableTreeNode temp_tree, String filter)
    {
        if (temp_tree.getChildCount()>0)
        {
            DefaultMutableTreeNode child = (DefaultMutableTreeNode)temp_tree.getFirstChild();
            while (child != null)
            {
                DefaultMutableTreeNode sibling = child.getNextSibling();
                applyCategoryFilter(child, filter);
                child = sibling;
            }
            if (temp_tree.getChildCount()==0)
                temp_tree.removeFromParent();
        }
        
        else
        {
            if (((String)temp_tree.getUserObject()).toLowerCase().indexOf(filter.toLowerCase())==-1)
                temp_tree.removeFromParent();
        }
    }
    
    private void jTextField1FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField1FocusGained
        if (jTextField1.getText().compareTo("Search...")==0)
            jTextField1.setText("");
    }//GEN-LAST:event_jTextField1FocusGained

    private void jTextField1FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField1FocusLost
        if (jTextField1.getText().compareTo("")==0)
            jTextField1.setText("Search...");
    }//GEN-LAST:event_jTextField1FocusLost

    private void jToggleButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jToggleButton1MouseClicked
        if (jToggleButton1.getText().compareTo("Category")==0)
        {
            jToggleButton1.setText("Individual");
            AppletState.setGroupToCategory(true);
        }
        else
        {
            jToggleButton1.setText("Category");
            AppletState.setGroupToCategory(false);
        }
        redraw();
    }//GEN-LAST:event_jToggleButton1MouseClicked

    private ArrayList expandedPaths(TreePath parent)
    {
        ArrayList rtnVal = new ArrayList();
        if (jTree1.isVisible(parent))
        {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode)parent.getLastPathComponent();
            Object[] tmp = ((DefaultMutableTreeNode)parent.getLastPathComponent()).getUserObjectPath();
            String[] tmp2 = new String[tmp.length-1];
            for (int inc=0; inc<tmp.length-1; inc++)
                tmp2[inc] = (String)tmp[inc];
            rtnVal.add(tmp2);
            if (node.getChildCount()>0)
            {
                Enumeration e = node.children();
                while (e.hasMoreElements())
                {
                    DefaultMutableTreeNode TN = (DefaultMutableTreeNode)e.nextElement();
                    TreePath path = parent.pathByAddingChild(TN);
                    rtnVal.addAll(expandedPaths(path));
                }
            }
        }
        return rtnVal;
    }
    
    private void jTree1ValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTree1ValueChanged
        TreePath[] paths = jTree1.getSelectionPaths();
        EventColorManager.reset();
        if (paths==null)
            return;
        for (int inc=0; inc<paths.length; inc++)
            EventColorManager.add(new EventColor((String)((DefaultMutableTreeNode)paths[inc].getLastPathComponent()).getUserObject()));
        
        ArrayList nameList = EventColorManager.getNameList();
        
        if (nameList.size()!=0)
        {
            String[] events = new String[nameList.size()];
            nameList.toArray(events);
            boolean tempStore = AppletState.isHeat();
            AppletState.setHeat(false);
            ArrayList result = new ArrayList();
            if (AppletState.getPrimarySearch()==0)
                result = HoneyData.groupBy(events, null, null, true, false, false);
            else
                result = HoneyData.groupBy(null, null, LocationManager.getLocations(events), false, false, true);
            EventColorManager.makeColors(result);
            AppletState.setHeat(tempStore);
        }
        
        redraw();
    }//GEN-LAST:event_jTree1ValueChanged

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        try
        {
            if (currentBarChart!=null)
                currentBarChart.doSaveAs();
        }
        catch (IOException e)
        {e.printStackTrace();}
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        try
        {
            if (currentLineChart!=null)
                currentLineChart.doSaveAs();
        }
        catch (IOException e)
        {e.printStackTrace();}
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    private void jMenuItem9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem9ActionPerformed
        AppletState.setTimeInterval(0);
        redraw();
    }//GEN-LAST:event_jMenuItem9ActionPerformed

    private void jMenuItem10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem10ActionPerformed
        AppletState.setTimeInterval(1);
        redraw();
    }//GEN-LAST:event_jMenuItem10ActionPerformed

    private void jMenuItem11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem11ActionPerformed
        AppletState.setTimeInterval(2);
        redraw();
    }//GEN-LAST:event_jMenuItem11ActionPerformed

    private void jMenuItem12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem12ActionPerformed
        AppletState.setTimeInterval(3);
        redraw();
    }//GEN-LAST:event_jMenuItem12ActionPerformed

    private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSlider1StateChanged
        AppletState.setSliderLocation(jSlider1.getValue());
        jPanel1.repaint();
    }//GEN-LAST:event_jSlider1StateChanged

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        timer.stop();
        jSlider1.setValue(0);
        jButton1.setIcon(play);
        jPanel1.repaint();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (timer.isRunning())
        {
            timer.stop();
            jButton1.setIcon(play);
        }
        else
        {
            timer.start();
            jButton1.setIcon(pause);
        }
        jPanel1.repaint();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jMenuItem18ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem18ActionPerformed
        AppletState.setSphereView();
        location_Tree("");
        ((DefaultTreeModel)jTree1.getModel()).setRoot(tree.getRoot());
        EventColorManager.changeMode();
        AppletState.setUpdate();
        redraw();
    }//GEN-LAST:event_jMenuItem18ActionPerformed

    private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyReleased
        if (jTextField1.getText().compareTo("")!=0)
        {
            if (AppletState.getPrimarySearch()==0)
                category_Tree(jTextField1.getText());
            else
                location_Tree(jTextField1.getText());
            ArrayList expanded = expandedPaths(new TreePath((TreeNode)jTree1.getModel().getRoot()));
            ((DefaultTreeModel)jTree1.getModel()).setRoot(tree.getRoot());
            for (int inc=0; inc<expanded.size(); inc++)
            {
                DefaultMutableTreeNode current = (DefaultMutableTreeNode)jTree1.getModel().getRoot();
                String[] arr = (String[])expanded.get(inc);
                boolean found = false;
                for (int incVal = 1; incVal<arr.length; incVal++)
                {
                    Enumeration e = current.children();
                    found = false;
                    while (e.hasMoreElements())
                    {
                        DefaultMutableTreeNode tmp = (DefaultMutableTreeNode)e.nextElement();
                        if (arr[incVal].compareTo((String)tmp.getUserObject())==0)
                        {
                            current = tmp;
                            found = true;
                        }
                    }
                    if (!found)
                        break;
                }
                if (found)
                    jTree1.expandPath(new TreePath(current.getPath()));
            }
        }
        else
        {
            if (AppletState.getPrimarySearch()==0)
                category_Tree(jTextField1.getText());
            else
                location_Tree(jTextField1.getText());
            ((DefaultTreeModel)jTree1.getModel()).setRoot(tree.getRoot());
        }
    }//GEN-LAST:event_jTextField1KeyReleased

    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
        AppletState.setMapType(2);
        redraw();
    }//GEN-LAST:event_jMenuItem7ActionPerformed

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
        AppletState.setMapType(1);
        redraw();
    }//GEN-LAST:event_jMenuItem6ActionPerformed

    private void jMenuItem17ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem17ActionPerformed
        JFrame frame = new ColorChooserPopup();
        frame.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                redraw();
            }
            
        });
        frame.setSize(new Dimension(300, 300));
        frame.setVisible(true);
        frame.pack();
    }//GEN-LAST:event_jMenuItem17ActionPerformed

    private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem8ActionPerformed
        AppletState.setHeat(true);
        redraw();
    }//GEN-LAST:event_jMenuItem8ActionPerformed

    private void jMenuItem13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem13ActionPerformed
        AppletState.setHeat(false);
        redraw();
    }//GEN-LAST:event_jMenuItem13ActionPerformed

    private void jMenuItem14ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem14ActionPerformed
        if (AppletState.isDistinct())
            AppletState.setDistinct(false);
        else
            AppletState.setDistinct(true);
        redraw();
    }//GEN-LAST:event_jMenuItem14ActionPerformed

    private void jMenuItem19ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem19ActionPerformed
        AppletState.setMapView();
        if (AppletState.getPrimarySearch()==0)
            category_Tree("");
        else
            location_Tree("");
        ((DefaultTreeModel)jTree1.getModel()).setRoot(tree.getRoot());
        EventColorManager.changeMode();
        AppletState.setUpdate();
        redraw();
    }//GEN-LAST:event_jMenuItem19ActionPerformed

    private void jMenuItem24ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem24ActionPerformed
        AppletState.setPrimaryViz(0);
        redraw();
    }//GEN-LAST:event_jMenuItem24ActionPerformed

    private void jMenuItem25ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem25ActionPerformed
        AppletState.setPrimaryViz(1);
        redraw();
    }//GEN-LAST:event_jMenuItem25ActionPerformed

    private void jMenuItem22ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem22ActionPerformed
        AppletState.setSecondaryViz(0);
        redraw();
    }//GEN-LAST:event_jMenuItem22ActionPerformed

    private void jMenuItem23ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem23ActionPerformed
        AppletState.setSecondaryViz(1);
        redraw();
    }//GEN-LAST:event_jMenuItem23ActionPerformed

    private void jMenuItem20ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem20ActionPerformed
        AppletState.setPrimarySearch(0);
        if (AppletState.getPrimarySearch()==0)
            category_Tree("");
        else
            location_Tree("");
        ((DefaultTreeModel)jTree1.getModel()).setRoot(tree.getRoot());
        EventColorManager.changeMode();
        AppletState.setUpdate();
        redraw();
    }//GEN-LAST:event_jMenuItem20ActionPerformed

    private void jMenuItem21ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem21ActionPerformed
        AppletState.setPrimarySearch(1);
        location_Tree("");
        ((DefaultTreeModel)jTree1.getModel()).setRoot(tree.getRoot());
        EventColorManager.changeMode();
        AppletState.setUpdate();
        redraw();
    }//GEN-LAST:event_jMenuItem21ActionPerformed

    private void jScrollPane4MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jScrollPane4MousePressed
        AppletState.setTopX(evt.getX());
        AppletState.setTopY(evt.getY());
    }//GEN-LAST:event_jScrollPane4MousePressed

    private void jScrollPane4MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jScrollPane4MouseReleased
        int x = evt.getX();
        int y = evt.getY();
        if (AppletState.getTopX() == -1)
            return;
        if (AppletState.getTopX() == x && AppletState.getTopY() == y)
        {
            AppletState.resetSelected();
            redraw();
            return;
        }
        if (AppletState.getTopX() > x )
        {
            AppletState.setBotX(AppletState.getTopX());
            AppletState.setTopX(x);
        }
        else
            AppletState.setBotX(x);
        if (AppletState.getTopY() > y)
        {
            AppletState.setBotY(AppletState.getTopY());
            AppletState.setTopY(y);
        }
        else
            AppletState.setBotY(y);
        if (AppletState.getPrimaryViz()!=0)
        {
            double[][] rtnVal = null;
            if (AppletState.getSphereCache()!=null)
                if (AppletState.getSphereCache().size()>0)
                {
                    int size = (int)((AppletState.getSizeX()<AppletState.getSizeY()?AppletState.getSizeY():AppletState.getSizeX())/1.5);
                    ArrayList rtnValX = new ArrayList();
                    ArrayList rtnValY = new ArrayList();
                    ArrayList sLocList = AppletState.getSphereCache();
                    for (int inc=0; inc<sLocList.size(); inc++)
                    {
                        SphereLocation SL = (SphereLocation)sLocList.get(inc);
                        double[][] vals = SL.rtnVals;
                        if (vals != null)
                            for (int inc2=0; inc2<vals.length; inc2++)
                            {
                                int xVal = (int)(vals[inc2][0] * size / 15 + AppletState.getSizeX()/2);
                                int yVal = (int)(vals[inc2][1] * size / 15 + AppletState.getSizeY()/2);
                                if (AppletState.getTopX() <= xVal && AppletState.getBotX() >= xVal && AppletState.getTopY() <= yVal && AppletState.getBotY() >= yVal)
                                {
                                    rtnValX.add(SL.latitude);
                                    rtnValY.add(SL.longitude);
                                    break;
                                }
                            }
                    }
                    rtnVal = new double[rtnValX.size()][2];
                    for (int inc=0; inc<rtnValX.size(); inc++)
                    {
                        rtnVal[inc][0] = (Double)rtnValX.get(inc);
                        rtnVal[inc][1] = (Double)rtnValY.get(inc);
                    }
                }
            AppletState.resetSelected();
            AppletState.setSelectedLocations(rtnVal);
        }
        redraw();
    }//GEN-LAST:event_jScrollPane4MouseReleased

    private void jMenuItem15ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem15ActionPerformed
        AppletState.setPrimaryStat(1);
        redraw();
    }//GEN-LAST:event_jMenuItem15ActionPerformed

    private void jMenuItem16ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem16ActionPerformed
        AppletState.setPrimaryStat(0);
        redraw();
    }//GEN-LAST:event_jMenuItem16ActionPerformed
    
    private void timerActionListener(java.awt.event.ActionEvent evt) {
        int tmp = jSlider1.getValue();
        if (tmp != 100)
            tmp += 1;
        else
            timer.stop();
        jSlider1.setValue(tmp);
    }
    
    private void redraw()
    {
        jScrollPane2.setViewportView(createStatsPanel());
        if (AppletState.getSecondaryViz()==1)
            jScrollPane3.setViewportView(createBarGraph());
        else
            jScrollPane3.setViewportView(createLineGraph());
        jPanel3.repaint();
        if (AppletState.getPrimaryViz()==0)
            jScrollPane4.setViewportView(new MapJPanel());
        else
            jScrollPane4.setViewportView(new SphereMetric());
        jScrollPane4.repaint();
        AppletState.setUpdate(false);
    }
    
    private DefaultMutableTreeNode tree;
    private ChartPanel currentBarChart, currentLineChart;
    private Timer timer;
    private Icon stop;
    private Icon play;
    private Icon pause;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu10;
    private javax.swing.JMenu jMenu11;
    private javax.swing.JMenu jMenu12;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenu jMenu6;
    private javax.swing.JMenu jMenu7;
    private javax.swing.JMenu jMenu8;
    private javax.swing.JMenu jMenu9;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem10;
    private javax.swing.JMenuItem jMenuItem11;
    private javax.swing.JMenuItem jMenuItem12;
    private javax.swing.JMenuItem jMenuItem13;
    private javax.swing.JMenuItem jMenuItem14;
    private javax.swing.JMenuItem jMenuItem15;
    private javax.swing.JMenuItem jMenuItem16;
    private javax.swing.JMenuItem jMenuItem17;
    private javax.swing.JMenuItem jMenuItem18;
    private javax.swing.JMenuItem jMenuItem19;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem20;
    private javax.swing.JMenuItem jMenuItem21;
    private javax.swing.JMenuItem jMenuItem22;
    private javax.swing.JMenuItem jMenuItem23;
    private javax.swing.JMenuItem jMenuItem24;
    private javax.swing.JMenuItem jMenuItem25;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItem9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JPopupMenu.Separator jSeparator3;
    private javax.swing.JPopupMenu.Separator jSeparator4;
    private javax.swing.JPopupMenu.Separator jSeparator5;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JTree jTree1;
    // End of variables declaration//GEN-END:variables
}
